<?php
require_once __DIR__.'/common/common.php';
require_once __DIR__.'/PHPExcel/Classes/PHPExcel.php';
$code=$_POST['code'];

$f=PHPExcel_IOFactory::createReader('Excel2007');
$f=PHPExcel_IOFactory::load('excel/'.$code.'.xlsx','2007');
//$f=PHPExcel_IOFactory::load('email.xlsx','2007');
$fc=$f->getSheet(0);
$data=array();
$i=1;
$arr=array(1=>'A','B','C');
while (true)
{
    $a=0;
    $tmp=array();
    foreach ($arr as $v)
    {
        $value = $fc->getCell($v.$i)->getValue();
        if ($value)
        {
            $tmp[]=$value;
            $a++;
        }else{
            $a--;
        }
    }
    if ($a==3)
    {
        $data[]=$tmp;
    }
    if ($a<0)
    {
        break;
    }
    $i++;
}
unset ($data[0]);
//var_export($data);
$data1=array();
foreach ($data as $value)
{

        if ($number=preg_match_all("/[a-z0-9A-Z_.]+@{1}[a-z0-9A-Z]+\.[a-zA-Z]+/",$value[0],$math)>0)
        {
            $sql1=addslashes($value[0]);
            $sql2=addslashes($value[1]);
            $sql3=addslashes($value[2]);
            $sql="INSERT INTO `mailbox`(postbox,title,content) VALUES ('$sql1','$sql2','$sql3')";
            mysqli_query($mysql,$sql);
            $index=mysqli_insert_id($mysql);//返回最后一条插入语句产生的自增 ID
            $data1[]=array('uid'=>$index,$value[0],$value[1],$value[2]);
        }
    //echo "<hr/>";
}
echo json_encode($data1);
/*for ($a=2;$str=$sheet->getCell('A'.$a)->getValue();$a++)
{
    if ($number=preg_match_all("/[a-z0-9A-Z_.]+@{1}[a-z0-9A-Z]+\.[a-zA-Z]+/",$str,$math)>0)
    {
        $sql1=$sheet->getCell('A'.$a)->getValue();
        $sql2=$sheet->getCell('B'.$a)->getValue();
        $sql3=$sheet->getCell('C'.$a)->getValue();
        $sql="INSERT INTO `mailbox`(postbox,title,content) VALUES ('$sql1','$sql2','$sql3')";
        //mysqli_query($mysql,$sql);
        echo $sql;
    }

}*/
/*$number=preg_match_all("/[a-z0-9A-Z_]+@{1}[a-z0-9A-Z]{2}.com/",
    '5698744@Qq.com',//对那些内容匹配
    $math);//匹配到的内容
echo $number;//返回匹配到的次数
echo "<hr/>";
var_export($math);*/